package com.exltsoft.demo.struts2.demo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.exltsoft.fc.action.admin.useradmin.UserDao;
import com.exltsoft.fc.entity.admin.User;
import com.opensymphony.xwork2.ActionSupport;


@Transactional
@Namespace("/demo")
//@Results( { @Result(name = "demo", location = "/pages/demo.jsp", type = "redirect") })
@Results( {	@Result(name = "download", type = "stream" , 
					params={"contentType", "application/vnd.ms-excel",
							"contentDisposition","attachment;filename=\"AllUsers.xls\"",
							"inputName","downloadFile"}),
			@Result(name = "demo", location = "/pages/demo.jsp", type = "redirect")})
@SuppressWarnings("serial")
public class DemoAction extends ActionSupport{
	
	
	private UserDao userDao;
	
	protected SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		
		return sessionFactory;
	}
	
	
	@Autowired
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}



	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public String execute() throws Exception {
		System.out.println("execute");
		//连接数据库的演示
//		User user = (User)sessionFactory.getCurrentSession().load(User.class, new Long(1));
		User user = userDao.get(new Long(1));
//		System.out.println(user.getUserid());
		return "demo";
	}
	
	//下载的演示
	public String download() throws Exception {
		System.out.println("download");
		return "download";
	}
	
	public InputStream getDownloadFile(){
		File file = new File("c:\\111.xls");
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return fis;
	}
}
